package com.sping_boot3.spirng_boot3.juc;

import lombok.Data;

@Data
public class Alternate_synchronized {
    private int anInt = 0;

    public synchronized void add(){
        while (anInt != 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        anInt++;
        this.notifyAll();
        System.out.println(Thread.currentThread().getName()+":1");
    }
    public synchronized void decr(){
      while (anInt == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        anInt--;
        this.notifyAll();
        System.out.println(Thread.currentThread().getName()+":0");
        System.out.println("----------------------------------");
    }
}
